package com.arofoxmsn.utils;

import java.util.ArrayList;
import java.util.List;

import com.arofoxmsn.model.User;
import com.arofoxmsn.model.location.Location;

public class MathTools {


//	
	public static boolean checkCercania(Location userLocation , Location newPoint, double radio){
		//41.6465481,2.7416344
		//41.6400037,2.7328352
		double radioPow = Math.pow(radio,2);
		double myLat = (newPoint.getLat() - userLocation.getLat());
		double myLong = newPoint.getLon() - userLocation.getLon();
		double pointCalc = Math.pow(myLat,2) + 
				Math.pow(myLong,2);
		System.out.println(radioPow);
		System.out.println(myLat);
		System.out.println(myLong);
		System.out.println(pointCalc);
		if(pointCalc <= radioPow){
			System.out.println(pointCalc+"***"+radioPow);
			System.out.println("si");
			return true;
		}
		System.out.println("No");
		return false;
	}
	/**
	 * it retrieves the distance between two gps points in globe
	 * @param userLocation
	 * @param newPoint
	 * @return
	 */
	public static double getDistance(Location userLocation , Location newPoint){
		//Testing values = 42 y pico de metros
		//41.6465481,2.7416344
		//41.6400037,2.7328352
		double earthRadio = 6371;
		
		double deltaLatitude = Math.toRadians((newPoint.getLat() - userLocation.getLat()));
		double deltaLong = Math.toRadians((newPoint.getLon() - userLocation.getLon()));
		double firtLat = Math.toRadians(userLocation.getLat());
		double secondLat = Math.toRadians(newPoint.getLat());
		
		double oneCalc = Math.sin(deltaLatitude/2) * Math.sin(deltaLatitude/2) +
				Math.sin(deltaLong/2) * Math.sin(deltaLong/2) * Math.cos(firtLat) * Math.cos(secondLat);
		
		double secondCalc = 2 * Math.atan2(Math.sqrt(oneCalc), Math.sqrt(1-oneCalc));
		
		return earthRadio * secondCalc;
		
	}
	
	/**
	 * this method is in charge of filter candidates for a given scope radio. We have to 
	 * take into account this
	 * @param cadidates
	 * @param userLocation
	 * @param radio
	 * @return
	 */
	public static List<User> filterUsers(List<User> cadidates, Location userLocation, double radio){
		List<User> listToReturn = new ArrayList<User>();
		for(User candidate : cadidates){
			double getMyFuckingDistance = getDistance(userLocation , candidate.getCurrentLocation());
			if( getMyFuckingDistance <= radio){
				listToReturn.add(candidate);
			}
		}
		return listToReturn;
	}
	
//http://www.movable-type.co.uk/scripts/latlong.html
//
//	var R = 6371; // km
//	var dLat = (lat2-lat1).toRad();
//	var dLon = (lon2-lon1).toRad();
//	var lat1 = lat1.toRad();
//	var lat2 = lat2.toRad();
//
//	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
//	        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
//	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
//	var d = R * c;
}
